package com.example.vw.tabletest.bean;

import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Lex lex on 2017/5/6.
 */

public class ReportInfo implements Parcelable {

    public static final String LOGTAG = "表清单获取";

    private String id;//"1538211396014",
    private String pid;//"6",
    private String text;//"C2052实测作物地块样本分布表",
    private String url;
    private String[] tags;//"Array[0],
    private boolean isChild = false;//"false,
    private boolean hasHtml =true;//"true,
    private boolean hasJS = false;//"false,
    private boolean isSelect =false;//"false


    public ReportInfo() {
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.id);
        dest.writeString(this.pid);
        dest.writeString(this.text);
        dest.writeArray(this.tags);
        dest.writeString(this.url);
        dest.writeInt(this.isChild?1:0);
        dest.writeInt(this.hasHtml?1:0);
        dest.writeInt(this.hasJS?1:0);
        dest.writeInt(this.isSelect?1:0);
    }

    protected ReportInfo(Parcel in) {
        this.id = in.readString();
        this.pid = in.readString();
        this.text = in.readString();
        this.url = in.readString();
//        this.tags = (String[])in.readArray();
        this.isChild = (in.readInt()==1);
        this.hasHtml = (in.readInt()==1);
        this.hasJS = (in.readInt()==1);
        this.isSelect = (in.readInt()==1);
    }

    public static final Creator<ReportInfo> CREATOR = new Creator<ReportInfo>() {
        @Override
        public ReportInfo createFromParcel(Parcel source) {
            return new ReportInfo(source);
        }

        @Override
        public ReportInfo[] newArray(int size) {
            return new ReportInfo[size];
        }
    };


    public static List<ReportInfo> loadFromJson(String strJson){
        try {
            JSONObject jsonObject = new JSONObject(strJson);
            final boolean isSuccess = jsonObject.getBoolean("success");
            if (isSuccess) {
                List<ReportInfo> reportInfos = new ArrayList<>();
                final String task = jsonObject.getString("data");
                Log.i(LOGTAG, "data:" + task);
                Gson gson = new Gson();
                JsonParser jsonParser = new JsonParser();
                JsonArray jsonArray = jsonParser.parse(task).getAsJsonArray();
                for (JsonElement element : jsonArray) {
                    ReportInfo info = gson.fromJson(element, ReportInfo.class);
                    if(info.isChild) {
                        reportInfos.add(info);
                    }
                }
                return reportInfos;
            } else {
                Log.i(LOGTAG,"服务器查询失败");
            }
        } catch (JSONException e) {
            Log.e(LOGTAG,"表单列表错误");
            e.printStackTrace();
        }
        return  null;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getPid() {
        return pid;
    }

    public void setPid(String pid) {
        this.pid = pid;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String[] getTags() {
        return tags;
    }

    public void setTags(String[] tags) {
        this.tags = tags;
    }

    public boolean isChild() {
        return isChild;
    }

    public void setChild(boolean child) {
        isChild = child;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public boolean isHasHtml() {
        return hasHtml;
    }

    public void setHasHtml(boolean hasHtml) {
        this.hasHtml = hasHtml;
    }

    public boolean isHasJS() {
        return hasJS;
    }

    public void setHasJS(boolean hasJS) {
        this.hasJS = hasJS;
    }

    public boolean isSelect() {
        return isSelect;
    }

    public void setSelect(boolean select) {
        isSelect = select;
    }
}

